package com.copa.l234;

import java.util.Stack;

/**
 * @author copa
 * @createDate 2022-05-31 09:10
 * @function 栈解法
 */
public class Test1 {

    public boolean isPalindrome(ListNode head) {
        // 这里定义一个栈，等会会进行一一比对
        Stack<Integer> stack = new Stack<>();
        ListNode temp = head;
        while (temp != null) {
            // 先遍历一遍head，且把值放入到stack里
            stack.push(temp.val);
            temp = temp.next;
        }
        temp = head;
        while (temp != null) {
            // stack依次取出来跟temp进行一一比对
            if (stack.pop() != temp.val) {
                return false;
            }
            temp = temp.next;
        }
        return true;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(2);
        head.next.next.next = new ListNode(1);
        head.next.next.next.next = new ListNode(1);
        System.out.println(new Test1().isPalindrome(head));
    }
}
